package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.SysRoleService;
import com.ktjiaoyu.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/role")
public class RoleController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/list")
    public String list(Model model) {
        try {
            //查询所有的角色信息
            List<SysRole> list = sysRoleService.findAll();
            model.addAttribute("list", list); //将角色信息放入model中
            return "role/list";
        } catch (Exception e) {
            LOGGER.error("RoleController中list方法执行发生错误", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 验证角色code是否唯一
     */
    @GetMapping("/codeExist")
    @ResponseBody
    public Map<String, Object> codeExist(@RequestParam("code") String code) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(code)) {
            map.put("error", "不能为空");
            return map;
        } else
            try {
                //调用service层方法验证角色code是否唯一
                boolean ui = sysRoleService.codeExist(code);
                if (ui) {
                    map.put("exist", 0);
                } else {
                    map.put("exist", 1);
                }
            } catch (Exception e) {
                LOGGER.error("RoleController中codeExist方法执行发生错误", e);
                throw new RuntimeException(e);
            }
        return map;
    }

    //跳转到添加页面
    @GetMapping("/save")
    public String toSave() {
        return "/role/save";
    }

    @GetMapping("/edit")
    public String toEdit(Model model, @RequestParam("id") Long id) {
        SysRole sysRole = null;
        try {
            //根据id查询角色信息
            sysRole = sysRoleService.findById(id);
            model.addAttribute("sysRole", sysRole);
        } catch (Exception e) {
            LOGGER.error("RoleController中toEdit方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return "role/save";
    }

    @PostMapping("/save")
    public String doSave(SysRole sysRole) {
        SysUser loginUser = session(Constants.SESSION_USER);
        try {
            if (sysRole.getId() == null) {
                //新增角色
                sysRole.setCreatedUserId(loginUser.getId());
                sysRole.setCreatedTime(new Date());
            } else {
                //修改角色
                sysRole.setUpdatedUserId(loginUser.getId());
                sysRole.setUpdatedTime(new Date());
            }

            //调用service层方法保存角色信息
            boolean ok = sysRoleService.save(sysRole);
            if (ok) {
                return redirect("/main/role/list");
            }
        } catch (Exception e) {
            LOGGER.error("RoleController中doSave方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return "role/save";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Map<String, Object> del(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            //调用service层方法删除角色信息
            boolean ok = sysRoleService.del(id);
            if (ok) {
                map.put("code", "200");
            } else {
                map.put("code", "501");
            }
        } catch (Exception e) {
            LOGGER.error("RoleController中del方法执行发生错误", e);
            throw new RuntimeException(e);
        }
        return map;
    }
}
